/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gmft.tablemanager;

import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 *
 * @author Jonnie T
 */
public class TeamDisplayHandler
{
    private String tblID;
    private GoogleFusionTableDB gft;
    
    //private final int teamValArr[]={0,50,100,1050,1100,9000};
    //private final String teamArr[]={"None","Blue (Pending)","Blue"
    //        ,"Red (Pending)","Red","Resource"};
    
    private final int teamValArr[]={0,10,11,12,100,101,102,10000,10100,11000};
    private final String teamArr[]={"None","Blue - Built","Blue - Activated",
        "Blue Target by Red","Red - Built","Red - Activated","Red - Targeted by Blue",
    "Resources","Destroyed","Competed - Blue/Red"};
    private final  int arrSize=teamArr.length;
    
    
    
    
    public TeamDisplayHandler(String _tblID)
    {
        tblID=_tblID;        
        //gft=new GoogleFusionTableDB(cFile);
        
    }
    
    public boolean authenticate(String credFile)
    {
        boolean authenticated=false;
        
        gft=new GoogleFusionTableDB(credFile);
        authenticated=true;       
        return authenticated;
    }
    
    
    
    public String blockSetTeamSQLQu(String rid, String team)
    {
       //integer relating to position in arrays for teamArr and teamValArr
       int tInt=-1;
       //String teamLoCase=team.toLowerCase();
       for (int i=0;i<arrSize;i++)
       {
          if (team.equalsIgnoreCase(teamArr[i]))
          {
              //Match position
              tInt=i;
              //Stop for loop
              i=arrSize;
          }          
       }
       
       return "UPDATE " + tblID + " SET TeamVal='" + teamValArr[tInt] +
               "', Team='" + teamArr[tInt] + "' WHERE ROWID='" + rid + "'";       
    }
    
    public boolean blockSetTeam(int row, int col, String team)
    {  
       int code = getCodeForSet(team);
                    
       if (code<0)//not -1 -> (can't find code)
           return false;
       
       String fieldName = "GridID";
       String gridID=row+"-"+col;
       
       //extract rowid where GridID = row-col
       String rid = gft.extractRowID(tblID,fieldName,gridID);
       
       String sqlCMD="UPDATE " + tblID + " SET TeamVal='" + teamValArr[code] +
          "', Team='" + teamArr[code] + "' WHERE ROWID='" + rid + "'";
       
       return gft.setQuery(sqlCMD,true);       
    }
    
    int getCodeForSet(String team)
    {
        //integer relating to position in arrays for teamArr and teamValArr
       int tInt=-1;
       //String teamLoCase=team.toLowerCase();
       for (int i=0;i<arrSize;i++)
       {
          if (team.equalsIgnoreCase(teamArr[i]))
          {
              //Match position
              tInt=i;
              //Stop for loop
              i=arrSize;
          }          
       }
       return tInt;
    }
    
}
